﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class Companies : ClassManager.SessionManagerPage
    {
        private void GetAllCompany()
        {
            CompanyGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.CompaniesProvider.GetAll();
        }       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.CompaniesColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.CompanyGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
            }
        }

        protected void CompanyGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetAllCompany();
            CompanyGrid.Height = Unit.Parse(HiddenFieldHeight.Value.ToString());
        }

        protected void CompanyGrid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.Companies CompanyEntity = ClassManager.InvokeEntityManager.Invoke_Company_Entity(e);
                CompanyEntity.CompanyId = GPSTrackingDBV2Custom.Data.DataRepository.CompaniesProvider.GetNextID();
                CompanyEntity.Guid = Guid.NewGuid().ToString();


                GPSTrackingDBV2.Data.DataRepository.CompaniesProvider.Insert(CompanyEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);            
                showErrorMessage("No se puede registrar la información.");
            }
        }

        protected void CompanyGrid_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.Companies CompanyEntity = ClassManager.InvokeEntityManager.Invoke_Company_Entity(e);
                CompanyEntity.OriginalCompanyId = CompanyEntity.CompanyId;

                GPSTrackingDBV2.Data.DataRepository.CompaniesProvider.Update(CompanyEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);                            
                showErrorMessage("No se puede actualizar la información.");
            }
        }

        protected void CompanyGrid_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.Companies CompanyEntity = ClassManager.InvokeEntityManager.Invoke_Company_Entity(e);

                GPSTrackingDBV2.Data.DataRepository.CompaniesProvider.Delete(CompanyEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);                            
                showErrorMessage("No se puede eliminar el registro porque la compañía se encuentra asociada a otros registros.");
            }
        }

        public  void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_miniError", "objWindowsMessageManager.show_miniError('" + msg + "','Compañías...');", true);
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            CompanyGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in CompanyGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }
            CompanyGrid.MasterTableView.Rebind();
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }
    }
}